<?php

/* 广告管理 Adv */

class BannerModel extends BaseModel
{

    var $table = 'banner';
    var $prikey = 'banner_id';
    var $_name = 'banner';
    var $expire = 7200;

    /* 添加编辑时自动验证 */
    var $_autov = array(
        'banner_name' => array(
            'required' => true, //必填
            'min' => 1, //最短1个字符
            'max' => 100, //最长100个字符
            'filter' => 'trim',
        ),
        'banner_url' => array(
            'required' => true, //必填
            'filter' => 'trim',
        ),
        'banner_sort' => array(
            'filter' => 'intval', //过滤
        ),
    );

    /**
     *   删除广告 
     */
    function drop($conditions, $fields = 'banner_pic')
    {
        import('img_server');
        $droped_rows = parent::drop($conditions, $fields);
        if ($droped_rows)
        {
            restore_error_handler();
            $droped_data = $this->getDroppedData();
            foreach ($droped_data as $key => $value)
            {
                if ($value['banner_pic'])
                {
                    $imageUrl = remove_image_server_host($value['banner_pic']);
                    @unlink(ROOT_PATH . '/' . $imageUrl);  //删除Logo文件
                }
            }
            reset_error_handler();
        }

        return $droped_rows;
    }

    /**
     *   获取广告 
     * @param array $banner_catid array():表示获取全部广告
     * @author libaoling
     */
    function _get_ad($banner_catid = array())
    {
        if (!is_array($banner_catid)){
            $banner_catid = array($banner_catid);
        }

        $cache_server = cache_server();
        $cacheKey = "banner_get_ad" . implode("_", $banner_catid);
        $arr = $cache_server->hGet($this->_name, $cacheKey);

        if ($arr === false)
        {
            $arr = array();
            $ad_key = array();
            if ($banner_catid)
            {
                $ad_key = $banner_catid;
            }
            else
            {
                require ROOT_PATH . '/api/adv.inc.php';
                $ad_key = array_keys($adv_data['banner_catid']);
            }
            $advs = $this->find(array(
                'conditions' => 'banner_status = 1 AND ' . db_create_in($ad_key, 'banner_catid'),
                'fields' => 'banner_id,banner_name,banner_pic,banner_catid,banner_url,banner_bg_color',
                'order' => 'banner_sort,banner_time asc',
            ));

            foreach ($advs as $val)
            {

                $arr[$val['banner_catid']][$val['banner_id']]['banner_id'] = $val['banner_id'];
                $arr[$val['banner_catid']][$val['banner_id']]['banner_name'] = $val['banner_name'];
                $arr[$val['banner_catid']][$val['banner_id']]['banner_pic'] = $val['banner_pic'];
                $arr[$val['banner_catid']][$val['banner_id']]['banner_url'] = $val['banner_url'];
                $arr[$val['banner_catid']][$val['banner_id']]['banner_bg_color'] = $val['banner_bg_color'];
            }
            
            $cache_server->hSet($this->_name, $cacheKey, $arr, $this->expire);
        }

        return $arr;
    }

    //清除模板缓存
    function clear_template_cache()
    {
        $cache_server = & cache_server();
        $keys = array(
            'wap_index_template'
        );
        foreach ($keys as $key)
        {
            $cache_server->delete($key);
        }

        $cache_server->delete($this->_name);
    }

}

?>